package com.tledu.mr;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;
import java.util.StringTokenizer;

public class MRPractice8 {
    /**
     * 设计kv形式, key 应该是id，value应该student对象
     * reduce阶段，输入key id ，value student对象 ，输出的key是id，value 基本信息
     */

    public static class MRPractice8Mapper extends Mapper<Object, Text, Text, Student> {

        private Text id = new Text();

        @Override
        protected void map(Object key, Text value, Context context) throws IOException, InterruptedException {
            // 获取文件名字，来判断是学生还是地理位置信息
            String fileName = ((FileSplit) context.getInputSplit()).getPath().getName();
            // 将一行数据切割成一个一个的字段
            System.out.println(fileName);
            String[] valArr = value.toString().split(",");
            Student stu;
            if ("student.txt".equals(fileName)) {
                // 学生信息
                stu = new Student(valArr[0], valArr[1], valArr[2], valArr[3]);
            } else {
                // 地理位置信息
                stu = Student.newStudentLocation(valArr[0], valArr[1], valArr[2], valArr[3]);
            }
            id.set(valArr[0]);
            context.write(id, stu);
        }
    }

    public static class MRPractice8Reducer extends Reducer<Text, Student, Text, Text> {
        @Override
        protected void reduce(Text key, Iterable<Student> values, Context context) throws IOException, InterruptedException {
            Student student = new Student();
            int i = 0;
            for (Student stu : values) {
                i++;
                student = Student.copy(student, stu);
            }
            if (i > 1) {
                // 说明两个值都存在
                context.write(key,new Text(student.toString()));
            }
        }

    }

    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        // driver就是驱动，将整个流程打通
        // 创建配置
        Configuration conf = new Configuration();
        // 创建任务
        Job job = Job.getInstance(conf, "job021-kxr-wordcount");

        // 运行的主类、mapper、combine、reducer
        job.setJarByClass(MRPractice8.class);
        job.setMapperClass(MRPractice8Mapper.class);

        // combiner还应该存在吗？ 因为局部聚合的话，会因为黑名单 的问题，导致数据无法进入reducer阶段，如果其它map还包含这个数据，这个数据就
        // 不会被黑名单筛选掉
//        job.setCombinerClass(WordCountReducer.class);
        job.setReducerClass(MRPractice8Reducer.class);

        // 配置输出的kv形式
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Student.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);

        // 指定输入和输出的目录
        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileInputFormat.addInputPath(job, new Path(args[1]));
        FileOutputFormat.setOutputPath(job, new Path(args[2]));

        // 开启任务，等待执行
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }

}
